﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DockSample.Line;
using System.Data.SQLite;

namespace DockSample.DAO
{
    class ComplexLineDAO : OriginalLineDAO
    {
        public void SaveComlpexLine(ComplexLine line, string parentFormId)
        {
            try
            {

                //           this.DeleteLine(line);
                SQLiteCommand cmd = this.GetCmd();
                StringBuilder sql = new StringBuilder("insert into OriginalLine(id,startPointX,startPointY,endPointX,endPointY,type,parentFormId,startDiagramId,endDiagramId,lineName");
                if (line.SubForm != null)
                {
                    sql.Append(",subFormId");
                }

                sql.Append(") values(@id,@startPointX,@startPointY,@endPointX,@endPointY,@type,@parentFormId,@startDiagramId,@endDiagramId,@lineName ");
                if (line.SubForm != null)
                {
                    sql.Append(",@subFormId");
                }
                sql.Append(")");
                cmd.CommandText = sql.ToString();
                cmd.Parameters.Add(new SQLiteParameter("id", line.id));
                cmd.Parameters.Add(new SQLiteParameter("startPointX", line.StartPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("startPointY", line.StartPoint.Y));
                cmd.Parameters.Add(new SQLiteParameter("endPointX", line.EndPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("endPointY", line.EndPoint.Y));

                cmd.Parameters.Add(new SQLiteParameter("type", line.GetType().ToString()));

                cmd.Parameters.Add(new SQLiteParameter("parentFormId", parentFormId));

                cmd.Parameters.Add(new SQLiteParameter("startDiagramId", line.StartDiagram.Id));
                cmd.Parameters.Add(new SQLiteParameter("endDiagramId", line.EndDiagram.Id));

                cmd.Parameters.Add(new SQLiteParameter("lineName", line.LineName));
                if (line.SubForm != null)
                {
                    cmd.Parameters.Add(new SQLiteParameter("subFormId", line.SubForm.id));
                }
                //          Console.Write(cmd.CommandText);
                cmd.ExecuteNonQuery();

                this.SavePartLine(line, parentFormId);

                //      return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                //      return false;
            }
            finally
            {
                this.conn.Close();
            }
        }


        private void SavePartLine(ComplexLine line, string parentFormId)
        {
            if (line.isComplex)
            {

                this.SaveStartLine(line.startLine, parentFormId);

                this.SaveEndLine(line.endLine, parentFormId);

                if (line.simpleLineList.Count > 0)
                {
                    foreach (SimpleLine sline in line.simpleLineList)
                    {
                        this.SaveSimpleLine(sline, parentFormId);
                    }
                }
            }
        }

        private void SaveStartLine(StartLine line, string parentFormId)
        {
            try
            {

                //           this.DeleteLine(line);
                SQLiteCommand cmd = this.GetCmd();
                StringBuilder sql = new StringBuilder("insert into OriginalLine(id,startPointX,startPointY,endPointX,endPointY,type,parentFormId,startDiagramId,complexLineId");
                if (line.SubForm != null)
                {
                    sql.Append(",subFormId");
                }

                sql.Append(") values(@id,@startPointX,@startPointY,@endPointX,@endPointY,@type,@parentFormId,@startDiagramId ,@complexLineId");
                if (line.SubForm != null)
                {
                    sql.Append(",@subFormId");
                }
                sql.Append(")");
                cmd.CommandText = sql.ToString();
                cmd.Parameters.Add(new SQLiteParameter("id", line.id));
                cmd.Parameters.Add(new SQLiteParameter("startPointX", line.StartPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("startPointY", line.StartPoint.Y));
                cmd.Parameters.Add(new SQLiteParameter("endPointX", line.EndPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("endPointY", line.EndPoint.Y));

                cmd.Parameters.Add(new SQLiteParameter("type", line.GetType().ToString()));

                cmd.Parameters.Add(new SQLiteParameter("parentFormId", parentFormId));

                cmd.Parameters.Add(new SQLiteParameter("startDiagramId", line.StartDiagram.Id));


                cmd.Parameters.Add(new SQLiteParameter("complexLineId", line.complexLineId));
                if (line.SubForm != null)
                {
                    cmd.Parameters.Add(new SQLiteParameter("subFormId", line.SubForm.id));
                }
                //          Console.Write(cmd.CommandText);
                cmd.ExecuteNonQuery();



                //      return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                //      return false;
            }
            finally
            {
                this.conn.Close();
            }

        }




        private void SaveEndLine(EndLine line, string parentFormId)
        {
            try
            {

                //           this.DeleteLine(line);
                SQLiteCommand cmd = this.GetCmd();
                StringBuilder sql = new StringBuilder("insert into OriginalLine(id,startPointX,startPointY,endPointX,endPointY,type,parentFormId,endDiagramId,complexLineId");
                if (line.SubForm != null)
                {
                    sql.Append(",subFormId");
                }

                sql.Append(") values(@id,@startPointX,@startPointY,@endPointX,@endPointY,@type,@parentFormId,@endDiagramId ,@complexLineId");
                if (line.SubForm != null)
                {
                    sql.Append(",@subFormId");
                }
                sql.Append(")");
                cmd.CommandText = sql.ToString();
                cmd.Parameters.Add(new SQLiteParameter("id", line.id));
                cmd.Parameters.Add(new SQLiteParameter("startPointX", line.StartPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("startPointY", line.StartPoint.Y));
                cmd.Parameters.Add(new SQLiteParameter("endPointX", line.EndPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("endPointY", line.EndPoint.Y));

                cmd.Parameters.Add(new SQLiteParameter("type", line.GetType().ToString()));

                cmd.Parameters.Add(new SQLiteParameter("parentFormId", parentFormId));

                cmd.Parameters.Add(new SQLiteParameter("endDiagramId", line.EndDiagram.Id));


                cmd.Parameters.Add(new SQLiteParameter("complexLineId", line.complexLineId));
                if (line.SubForm != null)
                {
                    cmd.Parameters.Add(new SQLiteParameter("subFormId", line.SubForm.id));
                }
       //             Console.Write(cmd.CommandText);
                cmd.ExecuteNonQuery();



                //      return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                //      return false;
            }
            finally
            {
                this.conn.Close();
            }

        }


        private void SaveSimpleLine(SimpleLine line, string parentFormId)
        {
            try
            {

                //           this.DeleteLine(line);
                SQLiteCommand cmd = this.GetCmd();
                StringBuilder sql = new StringBuilder("insert into OriginalLine(id,startPointX,startPointY,endPointX,endPointY,type,parentFormId,complexLineId");
                if (line.SubForm != null)
                {
                    sql.Append(",subFormId");
                }

                sql.Append(") values(@id,@startPointX,@startPointY,@endPointX,@endPointY,@type,@parentFormId,@complexLineId");
                if (line.SubForm != null)
                {
                    sql.Append(",@subFormId");
                }
                sql.Append(")");
                cmd.CommandText = sql.ToString();
                cmd.Parameters.Add(new SQLiteParameter("id", line.id));
                cmd.Parameters.Add(new SQLiteParameter("startPointX", line.StartPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("startPointY", line.StartPoint.Y));
                cmd.Parameters.Add(new SQLiteParameter("endPointX", line.EndPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("endPointY", line.EndPoint.Y));

                cmd.Parameters.Add(new SQLiteParameter("type", line.GetType().ToString()));

                cmd.Parameters.Add(new SQLiteParameter("parentFormId", parentFormId));

                cmd.Parameters.Add(new SQLiteParameter("complexLineId", line.complexLineId));
                if (line.SubForm != null)
                {
                    cmd.Parameters.Add(new SQLiteParameter("subFormId", line.SubForm.id));
                }
       //         Console.Write(cmd.CommandText);
                cmd.ExecuteNonQuery();



                //      return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                //      return false;
            }
            finally
            {
                this.conn.Close();
            }

        }
    }
}
